package com.jams.goodox.activity.serve.meet;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.TownApplicationLike;
import com.jams.goodox.adapter.viewHolder.StringBannerViewHolder;
import com.jams.goodox.entity.MeetRoomIntroduceBean;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ViewUtils;
import com.jams.goodox.widget.NoScrollGridView;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 场地详情页
 * Created by guozhen.hou on 2017/7/18.
 */

public class MeetDetailActivity extends BaseActivity {
    @Bind(R.id.tx_submit)
    TextView txSubmit;
    @Bind(R.id.convenientBanner_main)
    ConvenientBanner<String> convenientBannerMain;
    @Bind(R.id.tx_room_name)
    TextView txRoomName;
    @Bind(R.id.tx_in_people)
    TextView txInPeople;
    @Bind(R.id.tx_room_price)
    TextView txRoomPrice;
    @Bind(R.id.meet_facility_grid)
    NoScrollGridView meetFacilityGrid;
    @Bind(R.id.meet_service_grid)
    NoScrollGridView meetServiceGrid;
    @Bind(R.id.tx_pager_change)
    TextView txPagerChange;
    @Bind(R.id.tx_none_service)
    TextView txNoneService;
    @Bind(R.id.tx_none_facility)
    TextView txNoneFacility;

    private MeetRoomIntroduceBean meetRoomIntroduceBean;

    private String subId;
    private String busId;


    @Override
    protected int getLayoutId() {
        return R.layout.meet_detail_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "场地详情");
        meetRoomIntroduceBean = (MeetRoomIntroduceBean) getIntent().getSerializableExtra("meetRoomIntroduceBean");
        subId = getIntent().getStringExtra("subId");
        busId = getIntent().getStringExtra("busId");
        if (meetRoomIntroduceBean == null) return;
        ViewUtils.setRelativeLayoutWH(convenientBannerMain, RelativeLayout.LayoutParams.MATCH_PARENT, TownApplicationLike.width * 3 / 4);//设置焦点图大小
        convenientBannerMain.setPages(
                new CBViewHolderCreator<StringBannerViewHolder>() {
                    @Override
                    public StringBannerViewHolder createHolder() {
                        return new StringBannerViewHolder();
                    }
                }, meetRoomIntroduceBean.getPicList()).setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
        convenientBannerMain.setCanLoop(false);
        if (meetRoomIntroduceBean.getPicList().size() == 0) txPagerChange.setVisibility(View.GONE);
        txPagerChange.setText(String.format("%s/%s", 1, meetRoomIntroduceBean.getPicList().size()));
        convenientBannerMain.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                txPagerChange.setText(String.format("%s/%s", position + 1, meetRoomIntroduceBean.getPicList().size()));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        initMating();
        txInPeople.setText(meetRoomIntroduceBean.getCanInPeople());
        txRoomPrice.setText(meetRoomIntroduceBean.getRoomPrice());
        txRoomName.setText(meetRoomIntroduceBean.getRoomName());

        if (meetRoomIntroduceBean.getRoomMatings().size() == 0) {
            txNoneFacility.setVisibility(View.VISIBLE);
            meetFacilityGrid.setVisibility(View.GONE);
        } else {
            txNoneFacility.setVisibility(View.GONE);
            meetFacilityGrid.setVisibility(View.VISIBLE);
            meetFacilityGrid.setAdapter(new ListCommonAdapter<String>(this, meetRoomIntroduceBean.getRoomMatings(), R.layout.meet_mating_item) {
                @Override
                public void convert(ListCommonViewHolder helper, String item, int position) {
                    helper.setText(R.id.tx_mating, item);
                }
            });
        }

        if (meetRoomIntroduceBean.getMeetServiceList().size() == 0) {
            txNoneService.setVisibility(View.VISIBLE);
            meetServiceGrid.setVisibility(View.GONE);
        } else {
            txNoneService.setVisibility(View.GONE);
            meetServiceGrid.setVisibility(View.VISIBLE);
            meetServiceGrid.setAdapter(new ListCommonAdapter<String>(this, meetRoomIntroduceBean.getMeetServiceList(), R.layout.meet_mating_item) {
                @Override
                public void convert(ListCommonViewHolder helper, String item, int position) {
                    helper.setText(R.id.tx_mating, item);
                }
            });
        }

    }

    @OnClick(R.id.tx_submit)
    public void onViewClicked() {
        if (meetRoomIntroduceBean == null) return;
        Intent in = new Intent(this, MeetChooseSessionActivity.class);
        in.putExtra("id", meetRoomIntroduceBean.getId());
        in.putExtra("subId", subId);
        in.putExtra("busId", busId);
        startActivity(in);
    }

    private void initMating() {
        if (TownApplicationLike.width > 750) {
            meetFacilityGrid.setNumColumns(4);
            meetServiceGrid.setNumColumns(4);
        } else {
            meetFacilityGrid.setNumColumns(3);
            meetServiceGrid.setNumColumns(3);
        }
    }
}
